﻿using System;
using System.Configuration;
using System.Data;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using KPIS.DBM;
using KPIS.GERP.GFMS.MODEL;
using KPIS.GERP.GFMS.IDAL;

namespace KPIS.GERP.GFMS.DAL
{
    public class D_HRM_MAS_Districts : IDAL_HRM_MAS_Districts
    {
        DBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        ConvertNullable cn = new ConvertNullable();
        

        public D_HRM_MAS_Districts()
        {

        }

        public IList<M_HRM_MAS_Districts> GetFull(Nullable<int> country_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_MAS_Districts> infoList = new List<M_HRM_MAS_Districts>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@country_seq", country_seq);

                strSQL = "SELECT *"
                    + " FROM (SELECT d.DISTRICT_SEQ, d.DISTRICT_NAME, d.DISTRICT_NAME_ENG"
                        + ", a.AMP_NAME, p.PROV_NAME, c.COUNTRY_NAME"
                        + ", CONCAT(d.DISTRICT_NAME, ' ', a.AMP_NAME, ' ', p.PROV_NAME, ' ', c.COUNTRY_NAME) AS DISTRICT_TEXT"
                        + " FROM MAS_DISTRICTS d"
                        + " LEFT JOIN MAS_AMPHURS a ON d.AMP_SEQ = a.AMP_SEQ"
                        + " LEFT JOIN MAS_PROVINCES p ON a.PROV_SEQ = p.PROV_SEQ"
                        + " LEFT JOIN MAS_REGIONS r ON p.REGION_SEQ = r.REGION_SEQ"
                        + " LEFT JOIN MAS_COUNTRIES c ON r.COUNTRY_SEQ = c.COUNTRY_SEQ"
                        + " WHERE d.RECORD_STATUS not like @delete_status"
                        + (country_seq != null ? " and r.country_seq = @country_seq" : "")
                        + ") t"
                    //+ (strFilter.Length > 0 ? " WHERE DISTRICT_TEXT LIKE " + Misc.ValueForSQL("%" + strFilter + "%") : "")
                    + " ORDER BY COUNTRY_NAME, PROV_NAME, AMP_NAME, DISTRICT_NAME";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_MAS_Districts info = new M_HRM_MAS_Districts();
                        info.district_seq = Convert.ToInt32(drc[i]["district_seq"]);
                        info.district_name = drc[i]["district_name"].ToString();
                        info.district_name_eng = drc[i]["district_name_eng"].ToString();
                        info.amp_name = drc[i]["amp_name"].ToString();
                        info.prov_name = drc[i]["prov_name"].ToString();
                        info.country_name = drc[i]["country_name"].ToString();
                        info.district_text = drc[i]["district_text"].ToString();
                        info.record_status = drc[i]["record_status"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_HRM_MAS_Districts> GetList(Nullable<int> country_seq, string filter)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_MAS_Districts> infoList = new List<M_HRM_MAS_Districts>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(3);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@country_seq", country_seq);
                dbManager.AddParameters(2, "@filter", "%" + filter + "%");

                strSQL = "SELECT *"
                    + " FROM (SELECT d.DISTRICT_SEQ"
                        + ", d.DISTRICT_NAME, a.AMP_NAME, p.PROV_NAME, c.COUNTRY_NAME"
                        + ", CONCAT(d.DISTRICT_NAME, ' ', a.AMP_NAME, ' ', p.PROV_NAME, ' ', c.COUNTRY_NAME) AS DISTRICT_TEXT"
                        + " FROM MAS_DISTRICTS d"
                        + " LEFT JOIN MAS_AMPHURS a ON d.AMP_SEQ = a.AMP_SEQ"
                        + " LEFT JOIN MAS_PROVINCES p ON a.PROV_SEQ = p.PROV_SEQ"
                        + " LEFT JOIN MAS_REGIONS r ON p.REGION_SEQ = r.REGION_SEQ"
                        + " LEFT JOIN MAS_COUNTRIES c ON r.COUNTRY_SEQ = c.COUNTRY_SEQ"
                        + " WHERE d.RECORD_STATUS not like @delete_status"
                        + (country_seq != null ? " and r.country_seq = @country_seq" : "")
                        + ") t"
                        + (filter != null ? (filter.Length > 0 ? " WHERE DISTRICT_TEXT LIKE @filter" : "") : "")
                    + " ORDER BY COUNTRY_NAME, PROV_NAME, AMP_NAME, DISTRICT_NAME";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_MAS_Districts info = new M_HRM_MAS_Districts();
                        info.district_seq = Convert.ToInt32(drc[i]["district_seq"]);
                        info.district_text = drc[i]["district_text"].ToString();
                        info.district_name = drc[i]["district_name"].ToString();
                        info.amp_name = drc[i]["amp_name"].ToString();
                        info.prov_name = drc[i]["prov_name"].ToString();
                        info.country_name = drc[i]["country_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public IList<M_HRM_MAS_Districts> GetList(Nullable<int> amp_seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            IList<M_HRM_MAS_Districts> infoList = new List<M_HRM_MAS_Districts>();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@delete_status", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@amp_seq", amp_seq);

                strSQL = "SELECT d.DISTRICT_SEQ, d.DISTRICT_NAME"
                    + " FROM MAS_DISTRICTS d"
                    + " WHERE d.RECORD_STATUS not like @delete_status"
                    + (amp_seq != null ? " and d.amp_seq = @amp_seq" : "")
                    + " ORDER BY DISTRICT_NAME";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    for (int i = 0; i < drc.Count; i++)
                    {
                        M_HRM_MAS_Districts info = new M_HRM_MAS_Districts();
                        info.district_seq = Convert.ToInt32(drc[i]["district_seq"]);
                        info.district_name = drc[i]["district_name"].ToString();
                        infoList.Add(info);
                    }
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }

        public M_HRM_MAS_Districts GetInfo(int seq)
        {
            string strSQL;
            DataRowCollection drc = null;
            M_HRM_MAS_Districts info = new M_HRM_MAS_Districts();
            try
            {
                dbManager.Open();
                dbManager.CreateParameters(1);

                dbManager.AddParameters(0, "@seq", seq);

                strSQL = "SELECT d.DISTRICT_SEQ, d.DISTRICT_NAME, d.DISTRICT_NAME_ENG"
                        + ", a.AMP_NAME, p.PROV_NAME, c.COUNTRY_NAME"
                        + ", CONCAT(d.DISTRICT_NAME, ' ', a.AMP_NAME, ' ', p.PROV_NAME, ' ', c.COUNTRY_NAME) AS DISTRICT_TEXT"
                        + " FROM MAS_DISTRICTS d"
                        + " LEFT JOIN MAS_AMPHURS a ON d.AMP_SEQ = a.AMP_SEQ"
                        + " LEFT JOIN MAS_PROVINCES p ON a.PROV_SEQ = p.PROV_SEQ"
                        + " LEFT JOIN MAS_REGIONS r ON p.REGION_SEQ = r.REGION_SEQ"
                        + " LEFT JOIN MAS_COUNTRIES c ON r.COUNTRY_SEQ = c.COUNTRY_SEQ"
                        + " WHERE d.DISTRICT_SEQ = @seq";
                

                drc = dbManager.ExecuteDataSet(CommandType.Text, strSQL).Tables[0].Rows;
                if (drc.Count > 0)
                {
                    info.district_seq = Convert.ToInt32(drc[0]["district_seq"]);
                    info.district_name = drc[0]["district_name"].ToString();
                    info.district_name_eng = drc[0]["district_name_eng"].ToString();
                    info.amp_name = drc[0]["amp_name"].ToString();
                    info.prov_name = drc[0]["prov_name"].ToString();
                    info.country_name = drc[0]["country_name"].ToString();
                    info.district_text = drc[0]["district_text"].ToString();
                    info.record_status = drc[0]["record_status"].ToString();
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return info;
        }
    }
}
